
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#include "RTProcTextureThreshold.h"

using std::pair;
#include "RTPerlinNoise.h"

#include <math.h>

namespace RT
{
	/*Default constructor*/
	RTProcTextureThreshold::RTProcTextureThreshold(F32 persistance, I32 octave, F32 intensity):
	m_persistance(persistance), m_octave(octave), m_intensity(intensity){}

	/*Add a threshold to the texture*/
	void RTProcTextureThreshold::addThreshold(F32 threshold, const RTColor& c)
	{
		//if the threshold exists already, quit
		if(m_thresholds.find(threshold) != m_thresholds.end())
			return;

		//if the map is empty
		if(m_thresholds.empty())
		{
			m_thresholds.insert( pair<F32, RTColor> (threshold, c));
			return;
		}

		//insert the threshold at the right place
		map<F32, RTColor>::iterator i = m_thresholds.begin();
		map<F32, RTColor>::iterator next = i;

		while(i != m_thresholds.end())
		{
			if(threshold < i->first)
			{
				next = i;
				i = m_thresholds.end();
			}
			else
				i++;
		}

		m_thresholds.insert(next, pair<F32, RTColor>(threshold, c));

	
	}

	/*Calculate the color*/
	RTColor RTProcTextureThreshold::getColor(const RTVector3f& p)const
	{

		RTVector3f newP = m_transform * p;

		//get perlin value
		F32 perlin = m_intensity * RTPerlinNoise::perlin(newP, m_persistance, m_octave);
		perlin = fabs(perlin);

		//return RTColor(1, 0, 0) * perlin;
		//find upper threshold
		map<F32, RTColor>::const_iterator next = m_thresholds.begin();
		bool found = false;
		while(next != m_thresholds.end() && !found)
		{
			if(perlin < next->first)
				found = true;
			else
				next++;
		}

		//check first and last
		if(next == m_thresholds.begin())
			return next->second;

		if(next == m_thresholds.end())
		{
			map<F32, RTColor>::const_iterator temp = next;
			temp--;
			if(perlin >= temp->first)
				return temp->second;
		}

		//RTInterpolation
		map<F32, RTColor>::const_iterator previous = next;
		previous--;

		F32 f = (perlin - next->first) / (previous->first - next->first);

		return previous->second * (1 - f) + next->second * f;
	
	}

}